﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Web.Models;

namespace Web.Controllers
{
    [Authorize]
    public class RoomsController : Controller
    {
        private readonly ReservingRoomContext _db = new ReservingRoomContext();

        //
        // GET: /Rooms/
        [AllowAnonymous]
        public ActionResult Index()
        {
            return View(_db.Rooms.ToList());
        }

        //
        // GET: /Rooms/Details/5
        [AllowAnonymous]
        public ActionResult Details(int id = 0)
        {
            Room room = _db.Rooms.Find(id);
            if (room == null)
            {
                return HttpNotFound();
            }
            return View(room);
        }

        //
        // GET: /Rooms/Create
        public ActionResult Create()
        {
            var colors = new List<SelectListItem>
            {
                new SelectListItem { Text = "Red", Value = "0" },
                new SelectListItem { Text = "Blue", Value = "1"},
                new SelectListItem { Text = "Green", Value = "2"},
                new SelectListItem { Text = "Yellow", Value = "3" },
                new SelectListItem { Text = "Orange", Value = "4" },
                new SelectListItem { Text = "Violet", Value = "5" }
            };

            ViewBag.Colors = colors;

            return View();
        }

        //
        // POST: /Rooms/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                _db.Rooms.Add(room);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            var colors = new List<SelectListItem>
            {
                new SelectListItem { Text = "Red", Value = "0" },
                new SelectListItem { Text = "Blue", Value = "1"},
                new SelectListItem { Text = "Green", Value = "2"},
                new SelectListItem { Text = "Yellow", Value = "3" },
                new SelectListItem { Text = "Orange", Value = "4" },
                new SelectListItem { Text = "Violet", Value = "5" }
            };

            ViewBag.Colors = colors;

            return View(room);
        }

        //
        // GET: /Rooms/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Room room = _db.Rooms.Find(id);
            if (room == null)
            {
                return HttpNotFound();
            }

            var colors = new List<SelectListItem>
            {
                new SelectListItem { Text = "Red", Value = "0" },
                new SelectListItem { Text = "Blue", Value = "1"},
                new SelectListItem { Text = "Green", Value = "2"},
                new SelectListItem { Text = "Yellow", Value = "3" },
                new SelectListItem { Text = "Orange", Value = "4" },
                new SelectListItem { Text = "Violet", Value = "5" }
            };
            ViewBag.Colors = colors;

            return View(room);
        }

        //
        // POST: /Rooms/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Room room)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(room).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            var colors = new List<SelectListItem>
            {
                new SelectListItem { Text = "Red", Value = "0" },
                new SelectListItem { Text = "Blue", Value = "1"},
                new SelectListItem { Text = "Green", Value = "2"},
                new SelectListItem { Text = "Yellow", Value = "3" },
                new SelectListItem { Text = "Orange", Value = "4" },
                new SelectListItem { Text = "Violet", Value = "5" }
            };

            ViewBag.Colors = colors;

            return View(room);
        }

        //
        // GET: /Rooms/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Room room = _db.Rooms.Find(id);
            if (room == null)
            {
                return HttpNotFound();
            }
            return View(room);
        }

        //
        // POST: /Rooms/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Room room = _db.Rooms.Find(id);
            _db.Rooms.Remove(room);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}